#!/bin/bash

# 常用的Oh My Zsh插件
# 执行这个脚本可以安装这些插件:
# bash -c "$(curl -fsSL https://zconf.gitee.io/zsh/omz/plugins/)"

ZSH_CUSTOM=${ZSH_CUSTOM:-~/.oh-my-zsh/custom}
ZSH_CUSTOM_PLUGINS=$ZSH_CUSTOM/plugins

plugins=(
    zsh-syntax-highlighting
    zsh-autosuggestions
    )

download_from_zsh_users (){
    git clone https://github.com/zsh-users/$1.git $ZSH_CUSTOM_PLUGINS/$1
}

# 用于修复zsh-autosuggestions的bug
fix_autosuggestions() {
    grep -q '^pasteinit' ~/.zshrc || \
        echo '
# This speeds up pasting w/ autosuggest
# https://github.com/zsh-users/zsh-autosuggestions/issues/238
pasteinit() {
  OLD_SELF_INSERT=${${(s.:.)widgets[self-insert]}[2,3]}
  zle -N self-insert url-quote-magic
}


pastefinish() {
  zle -N self-insert $OLD_SELF_INSERT
}
zstyle :bracketed-paste-magic paste-init pasteinit
zstyle :bracketed-paste-magic paste-finish pastefinish
'   >> ~/.zshrc
    echo 'done fixing autosuggestions'
}


for name in ${plugins[@]}; do
    download_from_zsh_users $name && echo "$name downloaded"
    sed -i "s/^plugins=(/plugins=($name /" ~/.zshrc
    if [ $name = "zsh-autosuggestions" ]; then
        # 修复粘贴慢的bug
        fix_autosuggestions
    fi
done
